﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace test2
{
    public partial class client : Form
    {
        public client()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            init();
        }

        [DllImport("PipeModelDll.dll")]
        private static extern void StartPipeClient();
        [DllImport("PipeModelDll.dll")]
        private static extern  int SendPipeClientCmd(int cmd, byte[] buff, int len);
        [DllImport("PipeModelDll.dll")]
        private static extern  int RecvPipeClientCmd(ref int cmd, byte[] buf, ref int len);
        [DllImport("PipeModelDll.dll")]
        private static extern  void StopPipeClient();

        [DllImport("PipeModelDll.dll")]
        private static extern int GetClientStatus();

        private void init()
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // start
            StartPipeClient();

            Task t1 = new Task(RecvMsgTask);
            t1.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // stop
            StopPipeClient();
        }

        private byte seed = 0;
        private int cmd = 0;

        private void button3_Click(object sender, EventArgs e)
        {
            // send
            byte[] buf = new byte[2];
            buf[0] = seed++;
            buf[1] = (byte)cmd;
            SendPipeClientCmd(cmd++, buf, 2);
        }

        void RecvMsgTask()
        {
            int cmd = 0, len = 0;
            byte[] buf = new byte[2048];
            string text;
            int status, statusbk = -1;
            while (true)
            {
                if (RecvPipeClientCmd(ref cmd, buf, ref len) == 0)
                {
                    if (textBox1.InvokeRequired)
                    {
                        textBox1.Invoke((MethodInvoker)delegate
                        {
                            text = "recv: cmd: " + cmd.ToString() + "data: "+ buf[0].ToString() + " " + buf[1].ToString() + "\r\n";
                            textBox1.AppendText(text);
                        });
                    }
                }

                status = GetClientStatus();
                if (status != statusbk)
                {
                    statusbk = status;
                    switch (statusbk)
                    {
                        case 0:
                            if (textBox1.InvokeRequired)
                            {
                                textBox1.Invoke((MethodInvoker)delegate
                                {
                                    textBox1.AppendText("[I] client not start\n");
                                });
                            }
                            break;
                        case 1:
                            if (textBox1.InvokeRequired)
                            {
                                textBox1.Invoke((MethodInvoker)delegate
                                {
                                    textBox1.AppendText("[I] start client\n");
                                });
                            }
                            break;
                        case 2:
                            if (textBox1.InvokeRequired)
                            {
                                textBox1.Invoke((MethodInvoker)delegate
                                {
                                    textBox1.AppendText("[I] client connected\n");
                                });
                            }
                            break;
                    }
                }

                Thread.Sleep(10);
            }
        }
    }
}
